<template>
	<view class="bg">
		<view class="gifts">
			<view class="product">
				<view class="product-picture">
					<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/img/1.png" mode="aspectFill"></image>
				</view>
				<view class="product-information">
					<view class="name">
						高尔夫球杆哈哈哈哈
					</view>
					<view class="price">
						￥30
					</view>
				</view>
				<view class="modify-address" @click="xiuGai">
					<uni-icons type="compose" size="30" color="#000"></uni-icons>
				</view>
			</view>
			<view class="address">
				收货地址：广东省广州市天河区凌塘村凌塘新庄39号
			</view>
		</view>
		<uni-popup ref="duihuan" type="center">
			<view class="pop_con">
				<view class="hd f1">修改信息</view>
				<view class="gitf-address-content">
					<view class="gitf-address-content-input">
						<text class="w-20">姓名</text>
						<input type="text" v-model="userName" placeholder="请输入姓名">
					</view>
					<view class="gitf-address-content-input">
						<text class="w-20">手机号</text>
						<input type="number" v-model="phone" placeholder="请输入手机号">
					</view>
					<view class="gitf-address-content-input" @click="showCity = true">
						<text class="w-20">地区</text>
						<input type="text" disabled v-model="cityStr" placeholder="点击选择地区">
					</view>
					<view class="gitf-address-content-input">
						<text class="w-20">地址</text>
						<input type="text" v-model="addressInfo" placeholder="请输入详细地址">
					</view>
				</view>
				<view class="btns">
					<u-button text="取消" @click="closeDh"></u-button>
					<u-button type="success" text="确定" @click="onSubmit"></u-button>
				</view>
			</view>
		</uni-popup>
		<u-picker :show="showCity" @cancel="showCity = false" immediateChange ref="uPicker" :columns="cityList"
			@change="changeHandler" @confirm="setCity"></u-picker>
	</view>
</template>

<script>
	import address from './address.js';
	export default {
		data() {
			return {
				index: -1, //索引
				city: '', //选中城市,
				province: '', //选中省份
				userName: '', //收货人名字
				phone: '', //收货电话号码
				addressInfo: '', //收货地址详情
				orderInfo: {}, //订单详情
				orderID: -1, //订单id
				cityStr: '', //选择城市字符串
				cityList: [], //城市列表
				showCity: false, //是否展示城市弹出层
				addressList: [], //收货地址
				isAccept: false, //是否同意
				clientHeight: 348, //scroll的高度
				// 选中商品数组
				goodsList: []
			}
		},
		onLoad(e) {
			let add = address;
			let arr1 = add.map(item => item.name);
			let arr2 = add[0].children.map(item => {
				if (item.name == '市辖区' || item.name == '县') {
					return '--';
				} else {
					return item.name;
				}
			});
			this.cityList = [arr1, arr2];
		},
		methods: {
			// 选择城市
			setCity(e) {
				console.log('城市', e)
				if (e.value[1] === '--') {
					this.cityStr = e.value[0];
					this.city = e.value[0]
				} else {
					this.cityStr = e.value[0] + ' ' + e.value[1];
					this.province = e.value[0]
					this.city = e.value[1]
				}
				this.showCity = false;
			},
			// 城市选项发生改变逻辑
			changeHandler(e) {
				console.log(e);
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e;
				if (columnIndex === 0) {
					picker.setColumnValues(
						1,
						address[index].children.map(item => {
							if (item.name == '市辖区' || item.name == '县') {
								return '--';
							} else {
								return item.name;
							}
						})
					);
				}
			},
			xiuGai() {
				this.$refs.duihuan.open();
			},
			closeDh() {
				this.$refs.duihuan.close();
			},
			// 点击确认修改
			async onSubmit() {
				let {
					orderID,
					userName,
					addressInfo,
					phone,
					province,
					city
				} = this
				if (!userName) {
					this.$pv.msg('请输入名字')
				} else if (!phone) {
					this.$pv.msg('请输入手机号')
				} else if (!this.isPhone(phone)) {
					this.$pv.msg('请输入正确的手机号')
				} else if (!city) {
					this.$pv.msg('请选择地区')
				} else if (!addressInfo) {
					this.$pv.msg('请输入详细地址')
				} else {
					let res = await this.$api.post('Order/UpdateAddress', {
						ID: orderID,
						Delivery_Accepter: userName,
						Delivery_Address: this.cityStr + addressInfo,
						Delivery_Phone: phone,
					});
					this.$pv.msg('修改成功')
					this.$refs.duihuan.close();
				}
			},
		}
	}
</script>

<style lang="scss">
	.gifts {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding: 10rpx 40rpx;
		background-color: #fff;
		border-radius: 10rpx;

		.product {
			display: flex;

			.product-picture {
				width: 120rpx;
				height: 120rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.product-information {
				margin-left: 30rpx;

				.name {
					width: 200rpx;
					white-space: nowrap;
					/* 文本不换行 */
					overflow: hidden;
					/* 超出部分隐藏 */
					text-overflow: ellipsis;
					/* 超出部分显示省略号 */
					margin-top: 5rpx;
					color: #000;
				}

				.price {
					margin-top: 20rpx;
					font-size: 30rpx;
					color: #FF0000;
				}
			}

			.modify-address {
				margin-left: 250rpx;
				margin-top: 30rpx;
			}
		}

		.address {
			color: #000;
		}
	}

	.pop_con {
		height: 700rpx;
		background: #fff;
		width: 650rpx;
		border-radius: 16rpx;
		border: 1px solid #FEF0BC;
		padding-bottom: 10rpx;

		.gitf-address-content {
			box-sizing: border-box;
			width: 100%;
			padding: 0 30rpx;

			.gitf-address-content-input {
				margin-top: 20rpx;
				height: 70rpx;
				display: flex;
				align-items: center;

				.w-20 {
					width: 120rpx;
					text-align: left;
					line-height: 70rpx;
					color: #000;
				}

				input {
					height: 70rpx;
					width: 400rpx;
				}
			}
		}

		.hd {
			height: 120rpx;
			line-height: 120rpx;
			border-bottom: 1px solid #eee;
			text-align: center;
			line-height: 90rpx;
			color: #333;
			font-weight: bold;

		}

		.inp {
			margin-top: 30rpx;
			padding-left: 30rpx;
			padding-right: 30rpx;

			.lab {
				font-size: 32rpx;
				color: #666;
			}

			input {
				margin-top: 10rpx;
				height: 80rpx;
				line-height: 80rpx;
				width: 100%;
				padding-left: 10rpx;
				box-sizing: border-box;
				border: 1px solid #eee;
				border-radius: 10rpx;
				color: #000;
			}
		}

		.btns {
			margin-top: 40rpx;
			display: flex;
			padding-left: 30rpx;
			padding-right: 30rpx;
			justify-content: space-between;

			/deep/ .u-button__text {
				color: #666;
			}

			/deep/ .u-button--success {
				text {
					color: #fff !important;
				}

				background: linear-gradient(180deg, #FEF0BC 0%, #B98B51 100%);
			}

			/deep/ .u-button {
				width: 290rpx;
			}
		}
	}
</style>